<?php
namespace app\index\controller;
use think\Controller;
use app\admin\model\Type;
use app\admin\model\Banner;
use app\admin\model\Goods;
use app\index\model\Car;

class Index extends Controller
{
    private $typeModel;
    private $goodsModel;
    private $carModel;
    public function _initialize() {
        $this->typeModel = new Type();
        $this->goodsModel = new Goods();
        $this->carModel = new Car();
    }

    public function index()
    {
        // 购物车
        $uid = session('user')['id'];
        $cars = $this->carModel->where('uid',$uid)->select();
        $total_price = $this->carModel->where('uid',$uid)->sum('price');
        $shop_num = count($cars);
        session('cars',$cars);
        session('shop_num',$shop_num);

    	// 分类
    	$types = $this->typeModel->getNormalFirstType();
    	$childType = $this->typeModel->getChildType();

        // 饮品杯系列
        $drank = $this->typeModel->where(['parent_id'=>3,'status'=>['neq',-1]])->order(['listorder'=>'desc'])->select();
        // 饮品系列下的商品
        $drank_goods = $this->goodsModel->getGoodsByPid(3);

        // PET系列
        $pet = $this->typeModel->where(['parent_id'=>2,'status'=>['neq',-1]])->order(['listorder'=>'desc'])->select();
        // PET系列下的商品
        $pet_goods = $this->goodsModel->getGoodsByPid(2);

        // 纸杯系列
        $zhibei = $this->typeModel->where(['parent_id'=>1,'status'=>['neq',-1]])->order(['listorder'=>'desc'])->select();
        // 纸杯系列下的商品
        $zhibei_goods = $this->goodsModel->getGoodsByPid(1);

        // 生活用纸
        $paper = $this->typeModel->where(['parent_id'=>7,'status'=>['neq',-1]])->order(['listorder'=>'desc'])->select();
        // 生活用纸系列下的商品
        $paper_goods = $this->goodsModel->getGoodsByPid(7);
    	// 轮播图
    	$banner = new Banner();
    	$banners = $banner->getNormalBanner();

         // 新品
        $new = $this->goodsModel->getNewgoods();

        // 特惠品
        $discount = $this->goodsModel->getDiscount();

        return $this->fetch('',[
        	'types' => $types,
        	'childType' => $childType,
        	'banners' => $banners,
            'new' => $new,
            'discount' => $discount,
            'cars' => $cars,
            'shop_num' => $shop_num,
            'total_price' => $total_price,
            'drank' => $drank,
            'drank_goods' => $drank_goods,
            'pet' => $pet,
            'pet_goods' => $pet_goods,
            'zhibei' => $zhibei,
            'zhibei_goods' => $zhibei_goods,
            'paper' => $paper,
            'paper_goods' => $paper_goods,
        ]);


    }
}

